<?php

class Service extends AppModel {
  public $name = 'Service';

  public $validate = array(
      'Service.name' => array(
          'name' => 'notEmpty',
          'message'=>'Name is required'
      ),
      'Service.introduction' => array(
          'name' => 'notEmpty',
          'message'=>'Service introduction is required'
      )
  );
   //http://book.cakephp.org/1.3/view/1042/belongsTo

  function  beforeSave() {
    parent::beforeSave();
    if (empty($this->id)) {
      $url=Inflector::slug($this->data[$this->name]['name']);
      if(strlen($url)>100) {
        $id= substr($url, 0, 100);
      }
      $done=false;
      $count=1;
      if(!$done){
        $result = $this->find("first", array("conditions"=>array("Service.url"=>$url)));
        if (!empty($result)){
          $url .= "_".$count;
          $count++;
        } else {
          $done = true;
        }
      }
      $this->data[$this->name]['url']= $url;
    }
    return true;
  }
  
}

?>
